package com.rgs.se.data.recursion;

/**
 * @author Ren Gaoshuai
 * @ClassName ArraySum
 * 递归 数组求和
 * @date 2022-04-12 21:03
 **/
public class ArraySum {

    public static int sum(int[] arr) {
        return sum(arr, 0);
    }

    /**
     *计算 arr数组里面 l到最后的和
     */
    public static int sum(int[] arr, int l) {
        if (l == arr.length) {
            return 0;
        } else {
            return arr[l] + sum(arr, l + 1);
        }
    }

    public static void main(String[] args) {
        int[] nums = {1,2,3,4,5,6,7,8};
        System.out.println(sum(nums));
    }
}
